﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Department.admin.SiteStructure
{
    public partial class ViewAllMenus : System.Web.UI.Page
    {
       
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;
        int menuName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Всички менюта";

            AdminVariablesSingleton.changeCSS(Master, "struct");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Master, "viewMenus");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");
        }
        
        protected void markInactiveButton_Click(object sender, CommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();

            markAnItemInactive(ID);

            itemsView.DataBind();


        }

        private static void markAnItemInactive(string ID)
        {
            string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(strConnString))
            {
                try
                {
                    conn.Open();

                    //Get Site content
                    string sqlText = "UPDATE Menu SET IsHidden=1 WHERE ID = @ID";
                    SqlCommand comText = new SqlCommand(sqlText, conn);
                    comText.Parameters.AddWithValue("@ID", ID);

                    comText.ExecuteNonQuery();
                }

                catch (SqlException sqle)
                {
                    sqle.ToString();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {
            string item = e.CommandArgument.ToString();
       
                            string sqlText;
                            if (wasSortedDesc)
                            {
                                sqlText = "SELECT M.ID, M.Name, M.Position, M.IsHidden, M.ParentID, P.Name AS ParentName FROM Menu AS M INNER JOIN Menu AS P ON M.ParentID = P.ID ORDER BY " + item + " DESC";
                                AdminVariablesSingleton.WasSortedDesc = false;
                            }
                            else
                            {
                                sqlText = "SELECT M.ID, M.Name, M.Position, M.IsHidden, M.ParentID, P.Name AS ParentName FROM Menu AS M INNER JOIN Menu AS P ON M.ParentID = P.ID ORDER BY " + item;
                                AdminVariablesSingleton.WasSortedDesc = true;
                            }

            itemsViewDataSource.SelectCommand = sqlText;
            itemsView.DataBind();
           
        }

        protected void toggleall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkHeader = (CheckBox)sender;
            for (int i = 0; i < itemsView.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)itemsView.Items[i].FindControl("CheckBoxIsMarked");
                chk.Checked = chkHeader.Checked;
            }
        }
        protected void markAllInactive_Click(object sender, CommandEventArgs e)
        {
            foreach (ListViewItem item in itemsView.Items)
            {
                CheckBox isSelectedCheckBox = (CheckBox)itemsView.Items[item.DisplayIndex].FindControl("CheckBoxIsMarked");
                if (isSelectedCheckBox.Checked)
                {
                    markAnItemInactive(((Label)item.FindControl("IDLabel")).Text);
                }
            }
            itemsView.DataBind();
        }

        protected void UpdateListViewItem(object sender, ListViewUpdateEventArgs e)
        {
            string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
            ListViewItem item = itemsView.Items[e.ItemIndex];
            //int ID = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
            TextBox name = (TextBox)item.FindControl("NameTextBox");
            DropDownList menuName = (DropDownList)item.FindControl("menusList");
            TextBox position = (TextBox)item.FindControl("PositionTextBox");
            CheckBox isHidden = (CheckBox)item.FindControl("IsHiddenCheckBox");
            Label iD = (Label)item.FindControl("IDLabel");
            // insert records into database
            using (SqlConnection conn = new SqlConnection(strConnString))
            {
           //     UPDATE Menu SET Name = @Name, Position = @Position, IsHidden = @IsHidden, ParentID = @ParentID WHERE (ID = @ID)
                string Sql = itemsViewDataSource.UpdateCommand;
                conn.Open();
                using (SqlCommand dCmd = new SqlCommand(Sql, conn))
                {
                    dCmd.Parameters.AddWithValue("@Name", name.Text.Trim());
                    dCmd.Parameters.AddWithValue("@Position", position.Text.Trim());
                    dCmd.Parameters.AddWithValue("@isHidden", isHidden.Checked);
                    Object value;
                    if (menuName.SelectedValue.Equals("--"))
                    {
                        value = Convert.DBNull;
                    }
                    else
                    {
                        value = menuName.SelectedValue;
                    }
                    dCmd.Parameters.AddWithValue("@ParentID", value);
                    dCmd.Parameters.AddWithValue("@ID", iD.Text.Trim());
                    dCmd.ExecuteNonQuery();
                }
                conn.Close();
                
            }
            itemsView.EditIndex = -1;
            // Rebind the details
            itemsView.DataBind();
        }
        protected void EditListViewItem(object sender, ListViewEditEventArgs e)
        {
            itemsView.EditIndex = e.NewEditIndex;
            ListViewItem item = itemsView.Items[e.NewEditIndex];
            Label name = (Label)item.FindControl("ParentIDLabel");
            if (!name.Text.Trim().Equals(""))
            {
                menuName = Convert.ToInt32(name.Text.Trim());
            }
            // Rebind the details
            itemsView.DataBind();
        }
     
        protected void dropDownList_DataBound(object sender, EventArgs e)
        {
            //if (((DropDownList)sender).SelectedIndex.Equals("--"))
            //{
                ((DropDownList)sender).SelectedIndex = menuName;
          //  }
            
        }
   

    }
}